window.onload = function () {
    findAll(1)
}

function findAll(pageNo) {
    axios.get("stu/findAll", {
        params: {
            pageNo: pageNo,
            name: $("nameTxt").value,
            className: $("classTxt").value,
            roomName: $("roomTxt").value
        }
    }).then(resp => {
        var info = resp.data
        console.log(info)
        if (info === "noland"){
            location.href="/index.html?errorInfo=noland";
        }
        var str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.gender}</td><td>${n.classBean.nameClass}</td><td>${n.roomBean.address}</td><td>${n.phone}</td><td><input type="button" value="学生退房"onclick="del(${n.id}) " class="button12">
<input type="button" value="学生换房" onclick="update(${n.id})" class="button12"><input type="button" value="详情信息" onclick="find(${n.id})" class="button12"/></td></tr>`;
        })
        $("data").innerHTML = str;
        var pageStr = "";
        for (var i = 1; i <= info.totalPage; i++) {
            pageStr += `<a href="javascript:findAll(${i})" style="margin-left: 10px">${i} </a>`
        }
        $("cutDiv").innerHTML = pageStr;
    })
}

function find(id) {
    $("info").style.visibility = "visible"
    axios.get("/stu/findById", {
        params: {
            id: id
        }
    }).then(resp => {
        var info = resp.data
        $("nameSpan").innerHTML = info.name
        $("genderSpan").innerHTML = info.gender
        $("classSpan").innerHTML = info.classBean.nameClass
        $("roomSpam").innerHTML = info.roomBean.address
        $("dateSpan").innerHTML = info.date
        $("phoneSpan").innerHTML = info.phone
        $("faceImg").src = "/emFace/" + info.faceImg
    })
}

function affirm() {
    $("info").style.visibility = "hidden"
    $("add").style.visibility = "hidden"
}

function addstu() {
    $("add").style.visibility = "visible"
    axios.get("/class/findAll").then(rsep => {
        var info = rsep.data
        let str = "";
        info.forEach(n => {
            str += `<option value=${n.id}>${n.nameClass}</option>`
        })
        $("class").innerHTML = str;
    })
}

async function add() {
    var formObj = new FormData();
    formObj.append("uploadImg", $("fileTxt").files[0]);
    let config = {
        headers: {'Content-Type': 'multipart/form-data'}
    }
    var uploadFileName;
    //向服务器上传文件，得到上传文件名
    await axios.post("/upload", formObj, config).then(resp => {
        uploadFileName = resp.data;


        let sex;
        let obj = document.getElementsByName("sex");
        if (obj[0].checked) {
            sex = "男"
        } else {
            sex = "女"
        }
        axios.get("/stu/add", {
            params: {
                name:$("nameTxt1").value,
                gender: sex,
                phone: $("phone").value,
                chId: $("class").value,
                roId: $("room").value,
                faceImg: uploadFileName
            }
        }).then(resp=>{
           let info= resp.data
            if (info=="ok"){
                $("add").style.visibility = "hidden"
                findAll(1)
            }
        })
    })
}

function sex(type) {
    axios.get("/room/findByGender", {
        params: {
            gender: type
        }
    }).then(resp => {
        var info = resp.data
        str = ""
        info.forEach(n => {

            str += `<option value=${n.id}>${n.address}</option>`
        })
        $("room").innerHTML = str
    })
}

function changFace() {
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL($("fileTxt").files[0]);
    reader.onload = function (ev) {
        $("faceImg1").src = ev.target.result;
    }
}

function del(id){
    axios.get("/stu/del",{
        params:{
            id:id
        }
    }).then(resp=>{
        findAll(1)
    })
}


async  function update(id){
    var gender="";
    $("update").style.visibility="visible"
   await axios.get("/stu/findById",{
        params:{
            id:id
        }
    }).then(resp=>{
      var info= resp.data
            gender = info.gender
        console.log(gender)
        $("stuid").value=info.id
        $("nameSpan2").innerHTML=info.name
        $("genderSpan2").innerHTML=info.gender
        $("roomSpam2").innerHTML=info.roomBean.address
        $("classSpan2").innerHTML=info.classBean.nameClass
       $("faceImg2").src="/emFace/" + info.faceImg
    })
    axios.get("/room/findByGender",{
        params:{
            gender:gender
        }
    }).then(resp=>{
        str =""
         var info= resp.data
        console.log(info)
        info.forEach(n=>{
            str+= `<option value="${n.id}">${n.address}</option>`
        })
        $("room1").innerHTML=str;
    })

}
function confirm(){
    let rid = $("room1").value
    let sId = $("stuid").value
    axios.get("/stu/update",{
        params:{
            stuId:sId,
            roomId:rid
        }
    }).then(resp=>{
        $("update").style.visibility="hidden",
                findAll(1)
    })
}